// 文件下载，查看相关，与 nginx对应
/**
 * 下载文件（底稿附件）
 * @param filename
 * @param url
 */
// const filetypes = ['.txt', '.png', '.jpg', '.pdf', '.mp4', '.mp3'];

export const deleteDGs = async (paths: string[]): Promise<boolean> => {
  const url = `/delete_files?path=${paths.join(',')}`;
  const res = await fetch(url, {
    method: 'POST',
    headers: {
      Authorization: 'Basic dWZvc3l1c2VyOnp6aHhibQ==',
      'Content-Type': 'text/plain',
    },
  });
  const result = await res.text();
  return result === 'ok';
};

export const downloadDG = (url: string, fileName: string) => {
  // const filetype = path.extname(fileName);
  // if ((filetype !== '' && filetypes.includes(filetype)) || 'msSaveBlob' in navigator) {
  //   let href = url;
  //   try {
  //     // const { origin } = new URL(url);
  //     // href = url.replace(origin, '/files');
  //   } catch (err) {
  //     href = url;
  //   }
  //   fetch(href, {
  //     method: 'GET',
  //     // mode: 'cors',
  //     // credentials: 'include',
  //     headers: { Authorization: 'Basic dWZvc3l1c2VyOnp6aHhibQ==' },
  //   })
  //     .then(async (res) => {
  //       const text = await res.blob();
  //       const file = new Blob([text], { type: 'text/plain' });
  //       saveAs(file, fileName);
  //     })
  //     .catch((err) => {
  //       console.log(err);
  //     });
  // } else {
  let href = url.replace('http://', 'http://ufosyuser:zzhxbm@');
  href = href.replace('https://', 'https://ufosyuser:zzhxbm@');
  const link = document.createElement('a');
  link.style.display = 'none'; // 创建一个隐藏的a标签
  link.href = `${href}?fileName=${fileName}`;
  // link.download = fileName;
  // link.setAttribute('href', href); // 设置下载文件的url地址
  // link.setAttribute('download', aName);
  document.body.appendChild(link);
  link.click(); // 触发a标签的click事件
  document.body.removeChild(link);
  // }
};

/**
 * 使用在线查看工具打开底稿，支持在线提Q
 * @param jsonstr
 */
export const openDG = (jsonstr: string) => {
  // const href = `cpas://OpenFile/?url=${encodeURI(url)}&filename=${encodeURI(filename)}`;
  const href = `cpas://ViewDG/?dginfo=${encodeURI(jsonstr)}`;
  window.location.href = href;
};

/**
 * 只支持在线打开，不支持在线提Q等功能。
 * @param url
 * @param filename
 */
export const openFile = (url: string, filename: string) => {
  const href = `cpas://OpenFile/?url=${encodeURI(url)}&filename=${encodeURI(filename)}`;
  window.location.href = href;
};
